#include "actionmanager.h"
#include "aboutdialog.h"
#include "contactdialog.h"
#include "disclaimerdialog.h"
#include "libs/core/user/localuser.h"
#include "usersettings.h"
#include "yingce.h"
#include <QApplication>
#include <QDesktopServices>
#include <QFileDialog>
#include <QUrl>

ActionManager* ActionManager::instance_ = nullptr;

ActionManager::ActionManager(QObject* parent) : QObject { parent }
{
    instance_ = this;
    // Account
    login_action_ = new QAction(tr("login"), this);
    connect(login_action_, &QAction::triggered, this, [](bool checked) {
        Q_UNUSED(checked)
        YingCe::instance()->login();
    });
    register_account_action_ = new QAction(tr("Register Account"), this);
    connect(register_account_action_, &QAction::triggered, this, [](bool checked) {
        Q_UNUSED(checked)
        YingCe::instance()->registerUser();
    });
    edit_profile_action_ = new QAction(tr("Edit Profile"), this);
    connect(edit_profile_action_, &QAction::triggered, this, [](bool checked) {
        Q_UNUSED(checked)
        YingCe::instance()->editUserProfile();
    });
    change_password_action_ = new QAction(tr("Change Password"), this);
    connect(change_password_action_, &QAction::triggered, this, [](bool checked) {
        Q_UNUSED(checked)
        YingCe::instance()->changePassword();
    });
    delete_account_action_ = new QAction(tr("Delete Account"), this);
    connect(delete_account_action_, &QAction::triggered, this, [](bool checked) {
        Q_UNUSED(checked)
        YingCe::instance()->deleteUser();
    });
    logout_action_ = new QAction(tr("logout"), this);
    connect(logout_action_, &QAction::triggered, this, [](bool checked) {
        Q_UNUSED(checked)
        YingCe::instance()->logout();
    });
    // about
    about_action_ = new QAction(tr("&About YingCe..."), this);
    connect(about_action_, &QAction::triggered, this, [] {
        AboutDialog dlg(QApplication::activeWindow());
        dlg.resize(504, 226);
        dlg.exec();
    });
    // contactme
    contact_action_ = new QAction(tr("Contact"), this);
    connect(contact_action_, &QAction::triggered, this, [] {
        ContactDialog dlg(QApplication::activeWindow());
        dlg.resize(520, 580);
        dlg.exec();
    });
    // disclaimer
    disclaimer_action_ = new QAction(tr("Disclaimer"), this);
    connect(disclaimer_action_, &QAction::triggered, this, [] {
        DisclaimerDialog dlg(false, QApplication::activeWindow());
        dlg.exec();
    });
    // website
    website_action_ = new QAction(tr("&Visit YingCe Page..."), this);
    connect(website_action_, &QAction::triggered, this,
            [] { QDesktopServices::openUrl(QUrl("http://www.partsup.top/yingce")); });
    // check version
    check_version_action_ = new QAction(tr("Check Version"), this);
    connect(check_version_action_, &QAction::triggered, this,
            [] { YingCe::instance()->checkUpgrade(); });
    // exit
    exit_action_ = new QAction(tr("E&xit"), this);
    exit_action_->setShortcut(QKeySequence::Quit);
    connect(exit_action_, &QAction::triggered, this, [] { YingCe::instance()->quitApp(true); });
}

QMenu* ActionManager::buildFileMenu(QWidget* parent)
{
    QMenu* file_menu = new QMenu(tr("&File"), parent);
    file_menu->addSeparator();
    file_menu->addMenu(buildAccountMenu());
    file_menu->addSeparator();
    file_menu->addAction(exit_action_);
    return file_menu;
}

QMenu* ActionManager::buildAccountMenu(QWidget* parent)
{
    QMenu* account_menu = new QMenu(tr("Account"), parent);
    // account_menu->addAction(login_action_);
    account_menu->addAction(register_account_action_);
    account_menu->addAction(edit_profile_action_);
    account_menu->addAction(change_password_action_);
    account_menu->addAction(logout_action_);
    account_menu->addAction(delete_account_action_);
    connect(account_menu, &QMenu::aboutToShow, this, [this] {
        bool offline = YingCe::instance()->localUser()->offline();
        register_account_action_->setVisible(offline);
        login_action_->setVisible(offline);
        edit_profile_action_->setVisible(!offline);
        change_password_action_->setVisible(!offline);
        logout_action_->setVisible(!offline);
        logout_action_->setText(
                tr("logout(%1)").arg(YingCe::instance()->localUser()->displayName()));
        delete_account_action_->setVisible(!offline);
    });
    return account_menu;
}

QMenu* ActionManager::buildHelpMenu(QWidget* parent)
{
    QMenu* help_menu = new QMenu(tr("&Help"), parent);
    help_menu->addAction(website_action_);
    help_menu->addAction(check_version_action_);
    help_menu->addAction(about_action_);
    help_menu->addAction(contact_action_);
    help_menu->addAction(disclaimer_action_);
    connect(help_menu, &QMenu::aboutToShow, this, [this] {
        disclaimer_action_->setVisible(UserSettings::instance().disclaimerAccepted());
    });
    return help_menu;
}
